package com.digitalchina.lxnsy.activity.old;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;

import com.digitalchina.lxnsy.entity.TechnologiesVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.UserXML;
import com.facebook.drawee.view.SimpleDraweeView;
import com.zzhoujay.richtext.RichText;


import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/1/11 10:18
 * 修改备注：
 * ================================================
 */
public class TechnologiesDetailActivity extends BaseActivity{

    @Bind(R.id.title_tv)
    TextView tvTitle;
    @Bind(R.id.time_tv)
    TextView tvTime;
    @Bind(R.id.browse_tv)
    TextView tvBrowse;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.et_msg)
    EditText etMsg;
    @Bind(R.id.tv_send)
    TextView tvSend;
    @Bind(R.id.iv_logo)
    SimpleDraweeView ivLogo;

    private TechnologiesVO vo;
    private String id = "";

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_technologies_detail);
        setTitle("技术大全详情");
        RichText.initCacheDir(this);

    }

    @Override
    public void initMember() {

        if (getIntent().hasExtra("TechnologiesVO")) {
            vo = (TechnologiesVO) getIntent().getSerializableExtra("TechnologiesVO");
            id = vo.getId();
        } else {
            id = (String) getIntent().getSerializableExtra("contentId");
        }

        BusinessAccount.getTechnologiesDetail(this, id, mHandler);

    }

    @Override
    public void addListener() {

        tvSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!"1".equals(UserXML.getLoginStatus(TechnologiesDetailActivity.this))) {
                    GotoUtil.gotoActivity(TechnologiesDetailActivity.this, LoginGuideActivity.class);
                    return;
                }

                if (StringUtil.isEditEmpty(etMsg)) {
                    showToast("请输入评论内容");
                    return;
                }

                //BusinessTechnologies.publishComment(TechnologiesDetailActivity.this, id, etMsg.getText().toString(), "0", mHandler);
            }
        });
    }



    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_TECHNOLOGIES_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_PUBLISH_COMMENT_SUCCESS:
                        showToast("评论成功");
                        // 隐藏软键盘
                        InputMethodManager imm = (InputMethodManager) etMsg
                                .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        if (imm.isActive()) {
                            imm.hideSoftInputFromWindow(etMsg.getApplicationWindowToken(), 0);
                        }
                        etMsg.setText("");
                        break;
                    case MSG.MSG_TECHNOLOGIES_FIELD:
                    case MSG.MSG_PUBLISH_COMMENT_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        vo = FastJsonUtil.getBeanData(object.toString(), TechnologiesVO.class);
        FrescoUtil.showImageSmall(vo.getImg(), ivLogo);
        tvTitle.setText(vo.getTitle() != null ? vo.getTitle() : "");

        RichText.fromHtml(vo.getContent()).into(tvContent);

        //  tvContent.setText(vo.getContent() != null ? StringUtil.stripHtml(vo.getContent()) : "");
        tvTime.setText(vo.getCreateTime() != null ? vo.getCreateTime() : "");
        tvBrowse.setText(vo.getPageViewNums() != null ? vo.getPageViewNums() + "浏览" : "0浏览");


    }




    @Override
    public void rightOnClick() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
